//
//  MainMainVC.swift
//  GenerambaTest
//
//  Created by EWell on 13/03/2017.
//  Copyright © 2017 EWell. All rights reserved.
//

import UIKit

class MainVC: BaseVC {
    
    var centerBtn: UIButton!
    var tabBar: UIView!
    var items: [LGFImageTitleView]!
    var childVCSV: UIScrollView!
    var childVCs: [UIViewController] = []

    var selectIndex = 0
    
    public override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?){
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        super.isHiddenNavigationBar = true
    }
    public required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
        addNotification()
        addchildVCs()
    }

    func addchildVCs() -> Void {
        
        let vc1 = CourseVC()
        let vc2 = HzVC()
        let vc3 = GzVC()
        let vc4 = SbMainVC()
//        let vc4 = ScVC()
        let vc5 = WdVC()
        childVCs = [vc1, vc2, vc3, vc4, vc5]
        childVCs.forEach {
            self.addChild($0)
            $0.didMove(toParent: self)
        }
        selectLGFImageTitleView(items[0])
    }
    
    // 横竖屏控制
    override var shouldAutorotate: Bool {
        if LGF_ShouldAutorotate.lgf.lgf_IsShouldAutorotate {
            return true
        } else {
            return false
        }
    }
}

extension MainVC: LGFImageTitleViewDelegate {
    func selectLGFImageTitleView(_ view: LGFImageTitleView) {
        items.forEach { (item) in
            if item == view {
                item.isSelect = true
                selectIndex = item.tag
                childVCSV.setContentOffset(CGPoint.init(x: UIDevice.lgf_ScreenW * CGFloat(selectIndex), y: 0), animated: false)
                if let childVCView = childVCs[selectIndex].view {
                    if !childVCSV.subviews.contains(childVCView) {
                        childVCSV.addSubview(childVCView)
                        childVCView.snp.makeConstraints { (make) in
                            make.left.equalToSuperview().offset(CGFloat(selectIndex) * UIDevice.lgf_ScreenW)
                            make.centerY.height.equalToSuperview()
                            make.width.equalTo(UIDevice.lgf_ScreenW)
                        }
                    }
                }
            } else {
                item.isSelect = false
            }
        }
    }
}
